package com.lanou.horo.zhuizhui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.horo.zhuizhui.R;

import java.net.URLEncoder;

/**
 * Created by dllo on 15/11/5.
 */
public class WebViewActivity extends AppCompatActivity {
    private ImageView back;
    private TextView webViewtitle;
    private WebView everyDayWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_web);
        back = (ImageView) findViewById(R.id.webViewBackBtn);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        webViewtitle = (TextView) findViewById(R.id.webViewtitle);
        everyDayWebView = (WebView) findViewById(R.id.everyDayWebView);

        initData();


    }

    private void initData() {
        Intent intent = this.getIntent();
        webViewtitle.setText(intent.getStringExtra("title"));
        String weburl = intent.getStringExtra("url");
        everyDayWebView.loadUrl(weburl);
//        everyDayWebView.loadDataWithBaseURL(null, weburl, "text/html", "utf-8", null);
//        everyDayWebView.loadData(URLEncoder.encode(weburl, "utf-8"), "text/html", "utf-8");
//        everyDayWebView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                // TODO Auto-generated method stub
//                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                view.loadUrl(url);
//                return true;
//            }
//        });
//        WebView everyDayWebView = new WebView(this);
//        everyDayWebView.loadUrl("http://www.csdn.net");
//        WebSettings wSet = everyDayWebView.getSettings();
//        wSet.setJavaScriptEnabled(true);
        everyDayWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return true;
            }
        });
        WebSettings settings = everyDayWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (everyDayWebView.canGoBack()) {
                everyDayWebView.goBack();//返回上一页面
                return true;
            } else {
                System.exit(0);//退出程序
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
